package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.FlowTotalInfo;
import com.sl.au.entity.Module;
import com.sl.au.entity.User;
import com.sl.au.service.FlowTotalInfoService;

@RestController
@RequestMapping(value = "/ui/FlowTotalInfo")
public class FlowTotalInfoController extends BaseController {

	@Autowired
	FlowTotalInfoService flowTotalInfoService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		Module module = this.getPageModule("abnormalQry");
		ModelAndView modelAndView = this.createLayoutView("abnormalQry/abnormalindex");
		modelAndView.addObject("module", module);
		
		return modelAndView;
	}
	
	/**
	 * /得到树节点数据
	 */

	@RequestMapping(value = "getTree", method = RequestMethod.GET)
	public Map<String, Object> getTree() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> deviceTypeMap=new ArrayList<Map<String,Object>>();
		deviceTypeMap=flowTotalInfoService.getNodeMap();
		map.put("code", 1);
		map.put("deviceType", deviceTypeMap);
		return map;
	}
	
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> qMap = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();
		Map<String, Object>numMap=new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		Account a = this.getMyAccount();
		Module module = this.getPageModule("FlowTotalInfo");
		String dataFilter = module.getDataFilter();
		User user = a.getUser();
		this.getQueryParams(qMap,numMap);
		condition.put("dataFilter", dataFilter);
		condition.put("qMap", qMap);
		condition.put("numMap", numMap);
		List<FlowTotalInfo> contracts = flowTotalInfoService.getFlowTotalInfoHql(start, length, condition, user, "e.nFlowID,e.nOrder",
				"asc");
		long count=flowTotalInfoService.getFlowTotalInfoHql(0, Integer.MAX_VALUE, condition, user, "e.nFlowID,e.nOrder","asc").size();
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	private void getQueryParams(Map<String, Object> qMap,Map<String, Object>numMap){
		String names[]={"cContractNumber","cProductPlanNumber",
				"cOperateDes","cFlowConfigName","cFlowConfigDes",
				"cBusinessTypeName","cFlowName","cFlowDes",
				"cOrderUnitName","cProductType"};
		String dates[]={"dOperateTime"};
		String ns[]={"cEmpName", "cDeptName",};
		for(String name:names){
			if(StringUtils.hasText(request.getParameter(name))){
				numMap.put(name, request.getParameter(name));
			}
		}
		for(String n:ns){
			if(StringUtils.hasText(request.getParameter(n))){
				numMap.put(n, request.getParameter(n));
			}
		}
		for(String date:dates){
			if(null!=this.getDateQueryItem(date)){
				qMap.put(date,this.getDateQueryItem(date));
			}
		}
		if(null!=this.getIntegerQueryItem("nOrder")){
			qMap.put("nOrder",this.getIntegerQueryItem("nOrder"));
		}
		
		if(StringUtils.hasText(request.getParameter("id"))){
			numMap.put("id",request.getParameter("id"));
		}
	}
}
